//  ListController.m
//  tianyaClient
//
//  Created by Rocket on 11-7-25.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ListNetworkViewController.h"
#import "UIImageView+WebCache.h"
#import "UILabel+Custom.h"
#import "UIColor+HTML.h"
#import "tyconsts.h"

@implementation ListNetworkViewController

@synthesize mainTable=mainTable_;
@synthesize feeds=feeds_;
@synthesize parentViewController=parentViewController_;

- (CGRect)getViewFrame:(CGRect)rect
{
	if (self.hidesBottomBarWhenPushed) 
    {
		return CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height-kNavHeight);
	}
	return CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height-kNavHeight-kTabHeight);
}

- (CGRect)getTableViewFrame
{
	if (self.hidesBottomBarWhenPushed) 
    {
		return CGRectMake(0, 0, kScreenWidth, getScreenHeight() - kStatusHeight-kNavHeight);
	}
	return CGRectMake(0, 0, kScreenWidth, getScreenHeight() - kStatusHeight - kNavHeight - kTabHeight);
}

- (void)loadView 
{
	[super loadView];
    self.view.frame=[self getViewFrame:self.view.frame];
    
    CGRect tableFrame=[self getTableViewFrame];
	mainTable_ = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
	mainTable_.delegate = self;
	mainTable_.dataSource = self;
	mainTable_.backgroundColor = [UIColor clearColor];
	[self.view addSubview:mainTable_];
	super.pullTableView = mainTable_;
    
    [self setNavigationBackButton];
}

- (void)scrollTableToHead:(BOOL)animated
{
    if (feeds_ && [feeds_ count]>0)
    {
        NSIndexPath* indexPath = [NSIndexPath indexPathForRow:0 inSection:0];  
        [mainTable_ scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:animated]; 
    }
}

- (void)scrollTableToFoot:(BOOL)animated 
{  
    if (feeds_ && [feeds_ count]>0)
    {
        NSInteger section = [mainTable_ numberOfSections];  
        if (section<1) 
            return;  
        NSInteger row = [mainTable_ numberOfRowsInSection:section-1];  
        if (row<1) 
            return;  
        NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row-1 inSection:section-1];  
        [mainTable_ scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:animated]; 
    }
}  

- (void)onRefresh
{
    [self scrollTableToHead:FALSE];
    mainTable_.contentOffset=CGPointMake(0, -48);
    [self changeBackgroundViewFrame];
    [self startLoading];
}

- (BOOL)requestListData:(BOOL)refresh
{
    isRefresh_=refresh;
    return FALSE;
}

- (void)showGetMoreWaittingView
{
    if (getMoreWaittingView_==nil)
    {
        getMoreWaittingView_ = [UIButton buttonWithType:UIButtonTypeCustom];
        [getMoreWaittingView_ setImage:[UIImage imageNamed:@"more_message_expansion.png"] forState:UIControlStateNormal];
        CGRect rect=CGRectMake(0, mainTable_.frame.origin.y+mainTable_.frame.size.height-40, 320, 40);
        getMoreWaittingView_.frame = rect;
        //[getMoreWaittingView_ addTarget:self action:@selector(showMore) forControlEvents:UIControlEventTouchUpInside];
        UILabel* moreLabel=[[UILabel alloc] initWithFrame:CGRectMake(40, 0, 250, 40)];
        moreLabel.textColor=[UIColor whiteColor];
        moreLabel.text=@"内容加载中";
        moreLabel.backgroundColor=[UIColor clearColor];
        [getMoreWaittingView_ addSubview:moreLabel];
        [moreLabel release];
        [self.view addSubview:getMoreWaittingView_];//mainTable_.tableFooterView = getMoreWaittingView_;//
    }
    mainTable_.contentInset = UIEdgeInsetsMake(0, 0, 40, 0);
    getMoreWaittingView_.hidden=FALSE;
    [self showMore];
}

- (void)hideGetMoreWaittingView
{
    UIEdgeInsets edgeInsets=mainTable_.contentInset;
    edgeInsets.bottom=0;
    mainTable_.contentInset = edgeInsets;
    getMoreWaittingView_.hidden=TRUE;
}

- (void)showWaitingView
{
    CGRect frame=mainTable_.frame;
    firstLoadingView_=[[UIView alloc] initWithFrame:frame];
    firstLoadingView_.backgroundColor=[UIColor colorWithHTMLName:@"#f5f5f5"];
	LoadingView* loadingView=[[LoadingView alloc] initWithParentSize:frame.size withText:@"正在加载数据..." withStyle:ESmallStyle];
	[firstLoadingView_ addSubview:loadingView];
    [loadingView release];
	[loadingView startRotating];
    [self.view addSubview:firstLoadingView_];
}

- (void)hideWaitingView
{
	[firstLoadingView_ removeFromSuperview];
	[firstLoadingView_ release];
	firstLoadingView_=nil;
}

#pragma mark -
#pragma mark PullRefreshTableController

- (void)showMore
{
    if ([self requestListData:FALSE]) 
    {
        [super showMore];
    }
    else
    {
        [self performSelector:@selector(stopLoading) withObject:nil afterDelay:0.2];
        [self hideGetMoreWaittingView];
    }
}

- (void)refresh
{
    if ([self requestListData:TRUE])
    {
        [super refresh];
    }
    else
    {
        [self performSelector:@selector(stopLoading) withObject:nil afterDelay:0.2];
    }
}

#pragma mark -
#pragma mark UITableViewDelegate
- (NSInteger)numberOfSectionInTableView:(UITableView *)tableView {
	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	if (feeds_ == nil) {
		return 0;
	}
	return [feeds_ count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 44.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    
	static NSString *CellIdentifier = @"NetworkTableCell";    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                       reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.textLabel.text=@"需要重写该方法";
    //如果有图片
    // Here we use the new provided setImageWithURL: method to load the web image
    //[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
    //               placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
	[tableView deselectRowAtIndexPath:indexPath animated:FALSE];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate 
{
	[super scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
	if (!isLoading_) 
    {
		NSArray *visiblePaths = [mainTable_ indexPathsForVisibleRows];
		if([visiblePaths count] >= 1)
		{
			NSIndexPath *indexPath = [visiblePaths objectAtIndex:[visiblePaths count] - 1];
			if ([indexPath row] == [feeds_ count] - 1) 
            {
				//[self showMore];
                [self showGetMoreWaittingView];
			}
		}
	}
}

#pragma mark -
#pragma mark UIViewController

- (void)didReceiveMemoryWarning 
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	// terminate all pending download connections
}

- (void)viewDidUnload 
{
    getMoreWaittingView_=nil;
    [super viewDidUnload];
}

- (void)dealloc 
{
    [mainTable_ release];
    feeds_=nil;
    [super dealloc];
}

@end
